package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CustomerInfo;
import com.ruoyi.system.domain.CustomerChat;
import com.ruoyi.system.domain.DevicesData;
import com.ruoyi.system.mapper.CustomerInfoMapper;
import com.ruoyi.system.mapper.WaClientMapper;
import com.ruoyi.system.service.ICustomerInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RedisSyncService {

    private static final Logger log = LoggerFactory.getLogger(RedisSyncService.class);

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CustomerInfoMapper waUserMapper;

    @Autowired
    private WaClientMapper waClientMapper;

    @Autowired
    private ICustomerInfoService waUserService;


    /**
     * 同步设备状态到数据库
     */
    private void syncCustomerChat() {
        CustomerChat customerChat1 = new CustomerChat();
        customerChat1.setCustomerNumber(null);
        customerChat1.setCreateTime(null);

        List<CustomerChat> list111 = waClientMapper.selectClientTotal(customerChat1);
        list111.parallelStream().forEach(user -> {
            Object objext = redisCache.getCacheObject(user.getClientNumber() + "-send_message");
            if (StringUtils.isNotNull(objext)) {
                String status = String.valueOf(objext);
                if(user.getStatus() != Integer.valueOf(status)){
                    user.setStatus(Integer.valueOf(status));
                    waClientMapper.updateCustomerChat(user);
                }
            }
        });
        log.info("同步粉丝状态成功");
    }

    private void syncCusUser(List<DevicesData> devicesList) {
        devicesList.parallelStream().forEach(device -> {

            CustomerInfo cusUser = new CustomerInfo();
            cusUser.setContactName(device.getDeviceName());
//            cusUser.setLinkStatus(device.getDeviceState() + "");
            cusUser.setIpaddress(device.getIp());
            cusUser.setContactNumber(device.getDeviceId());

            CustomerChat customerChat = new CustomerChat();
            customerChat.setCustomerNumber(device.getDeviceName());
            customerChat.setCreateTime(null);

            List<CustomerChat> list3 = waClientMapper.selectClientTotal(customerChat);
//            List<CustomerChat> list1 = waClientMapper.selectSendTotal(customerChat);
            List<CustomerChat> list1 = list3.stream()
                    .filter(chat -> chat.getStatus() == 1)
                    .collect(Collectors.toList());

            customerChat.setCreateTime(new Date());
            List<CustomerChat> list4 = waClientMapper.selectToDayClientTotal(customerChat);

//            List<CustomerChat> list2 = waClientMapper.selectToDaySendTotal(customerChat);
            List<CustomerChat> list2 = list4.stream()
                    .filter(chat -> chat.getStatus() == 5)
                    .collect(Collectors.toList());


            String sendTotal = list1.size() + "";
            String toDaySendTotal = list2.size() + "";
            String clientTotal = list3.size() + "";
            String toDayClientTotal = list4.size() + "";

            cusUser.setSendTotal(sendTotal);
            cusUser.setToDaySendTotal(toDaySendTotal);
            cusUser.setClientTotal(clientTotal);
            cusUser.setToDayClientTotal(toDayClientTotal);

//            Object object = redisCache.getCacheObject(cusUser.getContactName() + ":status");
//            if (!String.valueOf(object).equals(cusUser.getLinkStatus())) {
//            cusUser.setLinkStatus(String.valueOf(object));
            waUserMapper.updateUserByContactNumber(cusUser);

//            }
        });
        log.info("同步设置状态成功");
    }

}

